<?php
Class Restaurant extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->data['controller'] = 'restaurant';
        $this->load->model('Mrestaurant');
        $this->checkAdmin($this->session->userdata('admin_id'), $this->session->userdata('permission_verify'));
        $this->_per = $this->checkPerMission($this->session->userdata('admin'), $this->session->userdata('SessionPermisson'), 15, $this->session->userdata('admin_id'));
        
    }
    
    public function index()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 50;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mrestaurant->getRetaurantDB($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        //echo "<pre>"; die(print_r($this->data['data']));

        $this->load->library('form_validation');
        $this->form_validation->set_rules('check-delete', 'Mục cần xóa', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $idArray = $this->getParamArray('checkboxs');
            $run = $this->Mrestaurant->deleteRetaurantDB($idArray);
            if(intval($run) == 200)
            {
                die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/restaurant/index";</script>');
            }
        }
        
        $this->data['action'] = 'listrestaurant';
        $this->load->view('template/layout', $this->data);
    }
    
    public function addrestaurant()
    {
        if($this->_per->add != 2)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', 'Tên nhà hàng', 'required');
        $this->form_validation->set_rules('img_hd_1', 'Logo đại diện', 'required');
        $this->form_validation->set_rules('img_hd_2', 'Ảnh đại diện', 'required');
        
        //$this->data['optionCountry'] = $this->Mrestaurant->optioncountry('');
        $this->data['danhmucGroupOtp'] = $this->Mrestaurant->danhmucGroupotp('');
        
        if($this->form_validation->run() === TRUE)
        {
            $time_folder = date('Ym');
            $path_mk = 'data/images/restaurant/'.$time_folder;
            $path = 'images/restaurant/'.$time_folder.'/';
            @mkdir(PROJECT_PATH.'/'.$path_mk, 0301);
            $arr_ing = array();
            for($i = 1;$i <= 3; $i++)
            {
                /*$data_image = '';
                $result = '';
                $dataimage = $this->getParamString('img_hd_'.$i);
                if(!in_array($dataimage, array(null, '', '0')))
                {
                    $timenow = time().rand(0,9999);
                    $nameimagess = date('Ym').'-nhatnv-'.$timenow.'.jpg';
                    $paths = PROJECT_PATH.'/data/images/restaurant/'.$time_folder.'/';
                    $arr_exp = array('[removed]', '[removed');
                    $exp = str_replace($arr_exp,'',$dataimage);
                    $result = $this->savefile($exp,$paths,$nameimagess);
                    if($result == 200)
                    {
                        $arr_ing[] = $path.$nameimagess;    
                    }else{
                        $arr_ing[] = 'images/other/error-img.jpg';
                    }
                }else{
                    $arr_ing[] = '';
                }*/
                
                $path1 = 'images/restaurant/';
    			@mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
    			$config['upload_path'] = '../data/'.$path1;
    			$config['allowed_types'] = 'gif|jpg|png';
    			$config['max_size']	= '2000';
    			$config['max_width']  = '10240';
    			$config['max_height']  = '7680';
    			$config['encrypt_name'] =  TRUE;// TRUE/FALSE (boolean)
    			$this->load->library('upload', $config);
    			if( !$this->upload->do_upload('img_uploads_'.$i)){
    				$arr_ing[] = '';
    			}else{
    				$data_img = $this->upload->data();
    				$arr_ing[] = $path1.$data_img['file_name'];
    			}	
            }
            
            $phone = $this->getParamString('phone');
            
            $run = $this->Mrestaurant->addRetaurantDb($this->session->userdata('admin_id'), $arr_ing[0], $arr_ing[1], $arr_ing[2], $phone);//, $arr_ing[3]);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thành công");window.location.href="'.$this->data['baseurl'].'manage/restaurant/index";</script>');
            }
        }
        $this->data['action'] = 'addrestaurant';
        $this->load->view('template/layout', $this->data);
    }
    
    public function editrestaurant()
    {
        if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->data['id'] = $this->getParamUri(4,1);
        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', 'Tên nhà hàng', 'required');
        
        $this->data['data'] = $this->Mrestaurant->getRestaurantDetailDB($this->data['id']);
        //echo "<pre>";
        //die(print_r($this->data['data']));

        $this->data['danhmucGroupOtp'] = $this->Mrestaurant->danhmucGroupotp($this->data['data']->categories);
        
        if($this->form_validation->run() === TRUE)
        {
            $time_folder = date('Ym');
            $path_mk = 'data/images/restaurant/'.$time_folder;
            $path = 'images/restaurant/'.$time_folder.'/';
            @mkdir(PROJECT_PATH.'/'.$path_mk, 0777);
            $arr_ing = array();
            for($i = 1;$i <= 3; $i++)
            {
                /*$data_image = '';
                $result = '';
                $dataimage = $this->getParamString('img_hd_'.$i);
                if(!in_array($dataimage, array(null, '', '0')))
                {
                    $timenow = time().rand(0,9999);
                    $nameimagess = date('Ym').'-nhatnv-'.$timenow.'.jpg';
                    $paths = PROJECT_PATH.'/data/images/restaurant/'.$time_folder.'/';
                    $arr_exp = array('[removed]', '[removed');
                    $exp = str_replace($arr_exp,'',$dataimage);
                    $result = $this->savefile($exp,$paths,$nameimagess);
                    if($result == 200)
                    {
                        $arr_ing[] = $path.$nameimagess;    
                    }else{
                        $arr_ing[] = 'images/other/error-img.jpg';
                    }
                }else{
                    $arr_ing[] = '';
                }*/
                
                $path1 = 'images/restaurant/';
    			@mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
    			$config['upload_path'] = '../data/'.$path1;
    			$config['allowed_types'] = 'gif|jpg|png';
    			$config['max_size']	= '2000';
    			$config['max_width']  = '10240';
    			$config['max_height']  = '7680';
    			$config['encrypt_name'] =  TRUE;// TRUE/FALSE (boolean)
    			$this->load->library('upload', $config);
    			if( !$this->upload->do_upload('img_uploads_'.$i)){
    				$arr_ing[] = '';
    			}else{
    				$data_img = $this->upload->data();
    				$arr_ing[] = $path1.$data_img['file_name'];
    			}	
            }
            
            $phone = $this->getParamString('phone');
            
            $run = $this->Mrestaurant->editRetaurantDb($this->session->userdata('admin_id'), $arr_ing[0], $arr_ing[1], $arr_ing[2], $this->data['id'], $phone);//, $arr_ing[3],
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/restaurant/index";</script>');
            }
        }
        $this->data['action'] = 'editrestaurant';
        $this->load->view('template/layout', $this->data);
    }
    
    public function deleterestaurant()
    {
        if($this->_per->del != 4)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $idArray = $this->getParamUri(4,1);
        $run = $this->Mrestaurant->deleteRetaurantDB($idArray);
        if(intval($run) == 200)
        {
            die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/restaurant/index";</script>');
        }
    }
    
    public function getzone()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        die($this->Mrestaurant->optionRestauRantZone($this->getParamInt('id')));
    }
    
    public function orderrestaurantcategories()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        $this->data['id_active'] = $this->getParamUri(4,3);
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mrestaurant->getRetaurantDB($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        //echo "<pre>"; die(print_r($this->data['data']));

        $this->load->library('form_validation');
        $this->form_validation->set_rules('check-delete', 'Mục cần xóa', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $idArray = $this->getParamArray('checkboxs');
            $run = $this->Mrestaurant->deleteRetaurantDB($idArray);
            if(intval($run) == 200)
            {
                die('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"><script>/*alert("Xóa thành công");*/window.location="'.$this->data['baseurl'].'manage/restaurant/index";</script>');
            }
        }
        
        $this->data['action'] = 'listrestaurant';
        $this->load->view('template/layout', $this->data);
    }
    
    
    public function sortlabel()
    {
        $arr = $this->getParamString('arr');
        die($this->Mrestaurant->sortLabalDb($arr));
    }
}